package com.tgb.controller;

import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.activiti.engine.RuntimeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.tgb.entity.Leave;
import com.tgb.service.LeaveManager;
/**
 * 申请请假的业务controller
 * @author hejingyuan
 *
 */
@Controller
public class LeaveController {

	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@Autowired
	protected LeaveManager leaveManager;//申请管理业务
	
    @Autowired
    RuntimeService runtimeService;//运行服务类
    

    /**
	 * 显示申请页面-何静媛2015年6月5日21:30:10
	 * 
	 * @param leave
	 */
	@RequestMapping(value = "/apply")
	public String apply() {			
		return "/leave/start";
	}
	

	/**
	 * 保存请假信息-2015年6月5日16:14:44
	 * 
	 * @param leave
	 */
	@RequestMapping(value = "start")
	public String startWorkflow(Leave leave,RedirectAttributes redirectAttributes,HttpServletRequest request) {		
		String name = request.getParameter("reason"); 
		/**
		 * 生成随机数，应用的申请的id中
		 */
		int id = new Random().nextInt(10);
		Long idl=new Long((long)id);
		leave.setId(idl);		
		leaveManager.saveLeave(leave);		
		redirectAttributes.addFlashAttribute("message", "业务数据已保存");	
		return "redirect:/query/"+id;
	}

	/**
	 * 根据id查询申请信息
	 * 
	 * @param leave
	 */
	@RequestMapping(value = "query/{id}")
	public ModelAndView queryApplyInfo(@PathVariable("id") Long id) {
		
		Leave leave=leaveManager.queryEntityById(id);	
		return new ModelAndView("/leave/audit", "leave", leave);
		
	}
}
